package models

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// Application 申请
type Application struct {
	Id         int64     `form:"id"`
	Title      string    `form:"title"`
	Type       int8      `form:"type"`
	Content    string    `form:"content"`
	ApplyorId  int64     `form:"applyorId"`
	ApplyTime  time.Time `orm:"auto_now_add"`
	HandlerId  int64     `form:"handlerId"`
	HandleTime time.Time `orm:"null"`
	State      int8      `orm:"default(0)"`
	CreateTime time.Time `orm:"auto_now_add"`
	UpdateTime time.Time `orm:"auto_now"`
	IsDelete   bool      `orm:"default(0)" json:"-"`
}

func (app *Application) TableName() string {
	return "t_application"
}

// NewApplication 创建一个申请
func NewApplication() *Application {
	return &Application{}
}

// Save 保存
func (app *Application) Save() (int64, error) {
	o := orm.NewOrm()
	return o.Insert(app)
}

// Update 更新指定字段
func (app *Application) Update(cols ...string) (int64, error) {
	o := orm.NewOrm()
	return o.Update(app, cols...)
}

// Delete 删除
func (app *Application) Delete() (int64, error) {
	o := orm.NewOrm()
	return o.Delete(app)
}

// DelLogic 逻辑删除
func (app *Application) DelLogic() error {
	if app.Id < 1 {
		return nil
	}
	o := orm.NewOrm()
	_, err := o.QueryTable(app).Filter(IdColumnName, app.Id).Update(orm.Params{
		"is_delete": 1,
	})
	return err
}

// GetById 根据id获取
func (app *Application) GetById() error {
	o := orm.NewOrm()
	return o.QueryTable(app).Filter("is_delete", 0).Filter("id", app.Id).One(app)
}
